<?php
namespace app\admin\controller;

use app\AdminCollection;
use My\Validate;
use My\Tool;
use app\model\Photosmodel;
use think\Db;

class Photos extends AdminCollection
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $input = input('');
        $page = isset($input['page']) ? $input['page'] : 1;
        $where = array();
        $list = Photosmodel::pageService($where);
        $orderkey = pagesize * ($page - 1);
        $_list = array();
        foreach ($list as $key => $val) {
            $_t = $val;
            $_t['orderkey'] = $orderkey += 1;
            $_t['inputtime'] = $val['inputtime'] != 0 ? date('Y-m-d H:i:s', $val['inputtime']) : '0000-00-00 00:00:00';
            $_t['deleteurl'] = url('photos/delete', array('id' => $val['id']));
            $_list[] = $_t;
        }
        $pages = $list->render();
        $this->assign('list', $_list);
        $this->assign('pages', $pages);
        return $this->fetch();
    }

    /**
     * 删除相册
     */
    public function delete()
    {
        try {
            $id = input('id');
            if (Validate::checkNull($id)) throw new \Exception("删除失败");
            $photosfind = Photosmodel::findService(['id' => $id]);
            if (Validate::checkEmpty($photosfind)) throw new \Exception("删除失败");
            $imagesurl = '.' . $photosfind['images_url'];
            if (file_exists($imagesurl)) unlink($imagesurl);
            $return = Photosmodel::deleteService(['id' => $id]);
            if ($return) {
                $this->returnSuccess("删除成功");
            } else {
                throw new \Exception("删除失败");
            }
        } catch (\Exception $e) {
            $this->returnError($e->getMessage());
        }
    }
}
